package com.cunzhang.server.service.impl;

import com.cunzhang.common.constants.MessageConstant;
import com.cunzhang.common.result.PageResult;
import com.cunzhang.pojo.dto.DishDTO;
import com.cunzhang.pojo.dto.DishPageQueryDTO;
import com.cunzhang.pojo.entity.Category;
import com.cunzhang.pojo.entity.Dish;
import com.cunzhang.pojo.entity.DishFlavor;
import com.cunzhang.pojo.vo.DishVO;
import com.cunzhang.server.mapper.CategoryMapper;
import com.cunzhang.server.mapper.DishFlavorMapper;
import com.cunzhang.server.mapper.DishMapper;
import com.cunzhang.server.service.DishService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQueryDish(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishMapper.pageQueryDish(dishPageQueryDTO.getCategoryId(), dishPageQueryDTO.getName(), dishPageQueryDTO.getStatus());
        PageResult pageResult = new PageResult();
        pageResult.setTotal(page.getTotal());
        pageResult.setRecords(page.getResult());
        return pageResult;
    }

    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO queryDishById(Long id) {
        Dish queryCondition = new Dish();
        queryCondition.setId(id);
        List<Dish> dishes = dishMapper.selectDish(queryCondition);
        if(dishes == null || dishes.size() != 1){
            throw new RuntimeException(MessageConstant.UNKNOWN_ERROR);
        }

        Dish dish = dishes.get(0);
        DishFlavor queryCondition1 = new DishFlavor();
        queryCondition1.setDishId(id);
        List<DishFlavor> dishFlavors = dishFlavorMapper.selectFlavor(queryCondition1);

        Category category = new Category();
        category.setId(dish.getCategoryId());
        List<Category> categoryList = categoryMapper.selectCategory(category);
        if(categoryList == null || categoryList.size() != 1){
            throw new RuntimeException(MessageConstant.UNKNOWN_ERROR);
        }

        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);
        dishVO.setCategoryName(categoryList.get(0).getName());
        return dishVO;
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Transactional
    @Override
    public void addDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.insertDish(dish);

        Long dishId = dish.getId();
        List<DishFlavor> flavorList = dishDTO.getFlavors();
        if(flavorList != null && !flavorList.isEmpty()){
            for(DishFlavor flavor : flavorList){
                flavor.setDishId(dishId);
            }
            dishFlavorMapper.insertFlavors(flavorList);
        }
    }

    /**
     * 修改菜品售卖状态
     * @param status
     * @param id
     */
    @Override
    public void changeStatus(Integer status, Long id) {
        Dish queryCondition = new Dish();
        queryCondition.setId(id);
        List<Dish> dishes = dishMapper.selectDish(queryCondition);
        if(dishes == null || dishes.size() != 1){
            throw new RuntimeException(MessageConstant.UNKNOWN_ERROR);
        }

        Dish dish = dishes.get(0);
        dish.setStatus(status);
        dishMapper.updateDish(dish);
    }

    /**
     * 批量删除菜品
     * @param idList
     */
    @Override
    @Transactional
    public void deleteDish(List<Integer> idList) {
        dishMapper.deleteDish(idList);
        dishFlavorMapper.deleteFlavor(idList);
    }

}
